package com.bbs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

@Configuration
public class GatewayConfig {
    @Configuration
    public class CorsConfig {
        @Bean
        public CorsWebFilter corsFilter() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            // 允许任何域名使用
            corsConfiguration.addAllowedOrigin("http://localhost:8084");
            // 允许任何头
            corsConfiguration.addAllowedHeader("*");
            // 允许任何方法（post、get等）
            corsConfiguration.addAllowedMethod("*");

            corsConfiguration.setAllowCredentials(true);

            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
            source.registerCorsConfiguration("/**", corsConfiguration);

            return new CorsWebFilter(source);
        }
    }
}
